<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <title>admin</title>

    <meta name="keywords" content="">
    <meta name="description" content="">
    <link rel="shortcut icon" href="/favicon.ico" />
    <!--[if lt IE 9]>
    <meta http-equiv="refresh" content="0;ie.html" />
    <![endif]-->

    <link rel="stylesheet" type="text/css" href="/static/css/admin/bootstrap.min14ed.css" />
	<link rel="stylesheet" type="text/css" href="/static/css/admin/font-awesome.min93e3.css" />
	<link rel="stylesheet" type="text/css" href="/static/css/admin/plugins/dataTables/dataTables.bootstrap.css">
	<link rel="stylesheet" type="text/css" href="/static/css/admin/animate.min.css" />
	<link rel="stylesheet" type="text/css" href="/static/css/admin/style.min862f.css"/>
    <link href="/static/css/admin/admincommon.css?v=20180712" rel='stylesheet' type='text/css' /> 
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>管理员列表</h5>
                        <if condition="check_auth('admin/User/addUser')">
                            <button id="toggle_modal"  class="btn btn-primary btn-circle" type="button" style="margin-bottom:0px;margin-left:10px;position:relative;top:-5px;" title="添加管理员">
                                <i class="fa fa-user-md"></i>+
                            </button>
                        </if>
                        <div class="ibox-tools">
                            <a href="javascript:location.replace(location.href);" class="btn btn-primary btn-circle" type="button" style="margin-bottom:0px;margin-right:10px;position:relative;top:-5px;" title="刷新">
                                <i class="fa fa-refresh"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <table class="table table-striped table-bordered table-hover dataTables-example">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>登入名</th>
                                    <th>手机</th>
                                    <th>邮箱</th>
                                    <th>角色</th>
                                    <th>加入时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <volist name="users" id="vo">
                                    <tr class="gradeX" id="user-{$vo['user_id']}">
                                        <td>{$vo['user_id']}</td>
                                        <td>{$vo['user_login']}</td>
                                        <td>{$vo['user_phone']}</td>
                                        <td class="center">{$vo['user_email']}</td>
                                        <td class="center">{$vo['role_name']}</td>
                                        <td class="center">{$vo['user_registered']}</td>
                                        <td class="center" align="center">
                                            <div class="btn-group">
                                                
                                                <if condition="$vo['user_id'] eq 1">
                                                    <if condition="$Request.session.user_id eq 1">
                                                        <button data-toggle="dropdown" class="btn btn-primary btn-xs dropdown-toggle " aria-expanded="false">操作 <span class="caret"></span>
                                                        </button>
                                                        <ul class="dropdown-menu">
                                                            <li>
                                                                <a href="javascript:;" class="font-bold edit-user" data-id="{$vo['user_id']}">修改</a>
                                                            </li>
                                                        </ul> 
                                                    <else/>
                                                        <button data-toggle="dropdown" class="btn btn-primary btn-xs disabled" aria-expanded="false">操作 <span class="caret"></span>
                                                        </button>
                                                    </if>
                                                <else/>
                                                    <button data-toggle="dropdown" class="btn btn-primary btn-xs dropdown-toggle" aria-expanded="false">操作 <span class="caret"></span>
                                                    </button>
                                                    <ul class="dropdown-menu">
                                                        <if condition="check_auth('admin/User/updateUser')">
                                                            <li><a href="javascript:;" class="font-bold edit-user" data-id="{$vo['user_id']}">修改</a></li>
                                                        </if>
                                                        <if condition="check_auth('admin/User/delUser')">
                                                            <li><a href="javascript:;" class="font-bold del-user" data-id="{$vo['user_id']}">删除</a></li>
                                                        </if>
                                                    </ul>
                                                </if>
                                            </div>
                                        </td>
                                    </tr>
                                </volist>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div id="user-modal-form" class="modal fade" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form id="user_form" action="" method="post" onsubmit="return false;">
                    <div class="modal-header">
                        <span class="refresh-btn" onclick="refresh_form('user_form');"><i class="fa fa-refresh"></i></span>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">添加管理员</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <label>登入名：</label>
                                    <input type="text" placeholder="输入登入名" name="user_login" value="" class="form-control">
                                </div>
                                <div class="form-group">
                                    <label id="user_pass_tip">密码：</label>
                                    <input type="password" placeholder="输入密码" name="user_pass" value="" class="form-control">
                                </div>
                                <div class="form-group">
                                    <label>电话：</label>
                                    <input type="text" placeholder="输入电话" name="user_phone" value="" class="form-control">
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <label>邮箱：</label>
                                    <input type="text" placeholder="输入邮箱" name="user_email" value="" class="form-control">
                                </div>
                                <div class="form-group">
                                    <label id="confirm_pass_tip">确认密码：</label>
                                    <input type="password" placeholder="确认密码" name="confirm_pass" value="" class="form-control">
                                </div>
                                <div class="form-group">
                                    <label>所属角色</label>
                                    <select class="form-control m-b" name="user_role" id="user_role">
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <input type="hidden" id="user_id" name="user_id" value="" />
                        <input type="hidden" id="handle_type"  name="handle_type"  value="add" />
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="submit" class="btn btn-primary" onclick="userSubmit();">确定</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <js href="/static/js/admin/jquery.min.js"/>
	<js href="/static/js/admin/bootstrap.min.js"/>
	<js href="/static/js/admin/plugins/dataTables/jquery.dataTables.js"/>
	<js href="/static/js/admin/plugins/dataTables/dataTables.bootstrap.js"/>
	<js href="/static/js/admin/content.min.js"/>
    <js href="/static/js/admin/plugins/layui/layer/layer.js"/>
    <script type="text/javascript" src="/static/js/admin/admincommon.js?v=20180712"></script>

    <script>
        $(document).ready(function(){
            $(".dataTables-example").dataTable();

            $(".ibox-content").on('click','.edit-user',function(){
                var userId = $(this).data("id"),option = '';
                
                $("#myModalLabel").text('编辑管理员');
                $("#user_pass_tip").text("新密码：");
                $("#confirm_pass_tip").text("确认新密码：");
                $.post("{:url('admin/User/getUser')}",{user_id:userId},function(data){
                    
                    if(data.code==1) {

                        $("input[name='user_login']").val(data.data.user_login);
                        $("input[name='user_pass']").val('');
                        $("input[name='confirm_pass']").val('');

                        $("input[name='user_phone']").val(data.data.user_phone);
                        $("input[name='user_email']").val(data.data.user_email);
                        $("#user_id").val(data.data.user_id);
                        $("#handle_type").val('update');
                        
                        option = '<option value="0">选择角色</option>';

                        for(var i in data.roles) {

                            if(data.roles[i].role_id == data.data.user_role) {

                                option+='<option value="'+data.roles[i].role_id+'" selected >'+data.roles[i].role_name+'</option>'; 

                            }else{
                              
                                option+='<option value="'+data.roles[i].role_id+'">'+data.roles[i].role_name+'</option>';
                            }
                           
                        }

                        $("#user_role").empty().append(option);

                    }else{

                        layer.msg(data.msg, {time:500, icon:5});  
                    }
                    
                    $("#user-modal-form").modal('show');

                },'json');

            });

            $(".ibox-content").on('click','.del-user',function(){
                
                var userId = $(this).data("id");

                layer.confirm("你确定要删除【"+$("#user-"+userId).children("td").eq(1).text()+"】吗？", {

                    btn: ['确定','取消'] //按钮

                }, function(){

                    $.post("{:url('admin/User/delUser')}",{user_id:userId},function(data){
                    
                        if(data.code==1) {

                            layer.msg(data.msg, {time:500, icon:1});
                             
                            $("#user-"+userId).remove();

                        }else{
                        
                            layer.msg(data.msg, {time:500, icon:2});

                        }

                    },'json');

                });
            });


            $("#toggle_modal").on('click',function(){
                var option = '';

                $("#myModalLabel").text('添加管理员');
                $("#user_pass_tip").text("密码：");
                $("#confirm_pass_tip").text("确认密码：");

                refresh_form('user_form');

                $.post("{:url('admin/User/getRoles')}",function(data){
                    option = '<option value="0">选择角色</option>';
                    if(data.code==1) {

                        for(var i in data.roles) {
                            option+='<option value="'+data.roles[i].role_id+'">'+data.roles[i].role_name+'</option>';
                        }
                    }
                    
                    $("#user_role").empty().append(option);

                },'json');

                $("#user_id").val('');
                $("#handle_type").val('add');
                $("#user-modal-form").modal('show');
            });

        });
        
        function userSubmit(){

            var handle_type = $("#handle_type").val();
            var data        = $("#user_form").serializeArray();
            var url         = '';
            
            if($.trim($("input[name='user_login']").val()) == '') {

                layer.msg('请填写登入名', {time:500, icon:5});  
                return false;
            }
            

            if(handle_type == 'add') {

                if($.trim($("input[name='user_pass']").val()) == '') {

                    layer.msg('请填写密码', {time:500, icon:5});  
                    return false;
                }

                if($.trim($("input[name='confirm_pass']").val()) != $.trim($("input[name='user_pass']").val())) {

                    layer.msg('确认密码不一致', {time:500, icon:5});  
                    return false;
                }

                url = "{:url('admin/User/addUser')}";

            }else if(handle_type == 'update'){

                if($.trim($("input[name='user_pass']").val())) {
                      
                    if($.trim($("input[name='confirm_pass']").val()) != $.trim($("input[name='user_pass']").val())) {
                  
                        layer.msg('确认密码不一致', {time:500, icon:5});  
                        return false;
                    }  
                }
                
                url = "{:url('admin/User/updateUser')}";
            }


            if($("select[name='user_role']").val() == 0) {

                layer.msg('请选择用户角色', {time:500, icon:5});  
                return false;
            }

            $.post(url,data,function(data){
                if(data.code==1) {
                   
                    layer.msg(data.msg, {time:500, icon:6}); 
                   
                    setTimeout(function(){

                       window.location.reload();//刷新当前iframe页面

                    },500);

                }else{
                    
                   layer.msg(data.msg, {time:500, icon:5});  
                }
            },'json');
        }
    </script>

</body>
</html>